﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Shweet.SPSolution.TeamsToFollow
{
    [ToolboxItemAttribute(false)]
    public class TeamsToFollow : WebPart
    {
        private Button _btnFollowTeam = null;
        private CheckBoxList _cblFollowingTeams = null;

        protected override void CreateChildControls()
        {
            _btnFollowTeam = new Button {Text = "Follow"};
            _btnFollowTeam.Click += new EventHandler(_btnFollowTeam_Click);
            
            List<Repository.Team> discoveredTeams = DiscoverShweetTeams();
            _cblFollowingTeams = new CheckBoxList();
            _cblFollowingTeams.DataSource = discoveredTeams;
            _cblFollowingTeams.DataTextField = "Name";
            _cblFollowingTeams.DataValueField = "Url";
            _cblFollowingTeams.DataBind();

            foreach (ListItem item in _cblFollowingTeams.Items)
                item.Selected = discoveredTeams.Find(t => t.Url == item.Value).Following;

            Controls.Add(_cblFollowingTeams);
            Controls.Add(_btnFollowTeam);
        }

        void _btnFollowTeam_Click(object sender, EventArgs e)
        {
            Repository.Teams teamsRepository = new Repository.Teams();
            Engine.Teams teams = new Engine.Teams(teamsRepository);
            List<Repository.Team> teamsToFollow = new List<Repository.Team>();
            List<Repository.Team> teamsToUnFollow = new List<Repository.Team>();

            foreach (ListItem item in _cblFollowingTeams.Items)
            {
                if (item.Selected)
                {
                    teamsToFollow.Add(new Repository.Team()
                    {
                        Name = item.Text,
                        Url = item.Value
                    });
                }
                else
                {
                    teamsToUnFollow.Add(new Repository.Team()
                    {
                        Name = item.Text,
                        Url = item.Value
                    });
                }
            }

            teams.FollowShweetTeams(teamsToFollow);
            teams.UnFollowShweetTeams(teamsToUnFollow);
        }

        private List<Repository.Team> DiscoverShweetTeams()
        {
            Repository.Teams teamsRepository = new Repository.Teams();
            Engine.Teams teams = new Engine.Teams(teamsRepository);
            return teams.DiscoverShweetTeams();
        }


        private List<Repository.Team> FollowingShweetTeams()
        {
            Repository.Teams teamsRepository = new Repository.Teams();
            Engine.Teams teams = new Engine.Teams(teamsRepository);
            return teams.GetFollowing();
        }
     }
}
